package com.keyancompany.zhongbaopingtai.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.utils.EditTextUtils;
import com.keyancompany.zhongbaopingtai.utils.SPUtils;
import com.keyancompany.zhongbaopingtai.utils.T;
import com.keyancompany.zhongbaopingtai.weight.CustomEditText;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 新建标签
 * Created by Gia on 2016/8/13.
 */
public class CreateLabelActivity extends BaseActivity {

	@Bind(R.id.toolbar)
	Toolbar toolbar;

	@Bind(R.id.name)
	CustomEditText name;

	@Bind(R.id.color)
	CustomEditText color;

	@Bind(R.id.text_cancle)
	TextView textCancle;

	@Bind(R.id.text_sure)
	TextView textSure;

	@BindString(R.string.label_create)
	String projectTitle;

	@Override
	protected int getContentViewId() {
		return R.layout.activity_create_label;
	}

	@Override
	protected int getMenuId() {
		return 0;
	}

	@Override
	protected void initToolBar() {
		initToolBar(toolbar, projectTitle);
	}

	@Override
	protected void setUpViews() {

	}


	@Override
	public void success(int type, String json) {
		super.success(type, json);
		BaseModel base = JSONObject.parseObject(json, BaseModel.class);
		if (base == null) {
			T.showShort(mContext, base.msg);
			return;
		}
		switch (type) {
			case TeamAPI.CHECK_LABEL_MARK:

				if (isNetvaiable()) {

					String teamCode = SPUtils.getComment(mContext).getString(Constants.TEAM_CODE, null);
					TeamAPI.labelsave(teamCode, name.getText().toString().trim(),
						color.getText().toString().trim(), CreateLabelActivity.this, getLoading(-1));
				}
				break;
			case TeamAPI.LABEL_SAVE_MARK:

				break;
		}

	}

	@Override
	public void failed(String error) {
		super.failed(error);

		T.showShort(mContext, error);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// TODO: add setContentView(...) invocation
		ButterKnife.bind(this);
	}

	@OnClick({R.id.text_cancle, R.id.text_sure})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.text_cancle:
				finish();
				break;
			case R.id.text_sure:
				if (EditTextUtils.getEditTextValue(name) == null) {
					T.showLong(mContext, R.string.label_name_empty_hint);
					return;
				}
				if (EditTextUtils.getEditTextValue(color) == null) {
					T.showLong(mContext, R.string.label_color_empty_hint);
					return;
				}

				if (isNetvaiable()) {
					TeamAPI.checkLabel(name.getText().toString().trim(), CreateLabelActivity.this, getLoading(-1));
				}
				break;
		}
	}
}
